import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: { hidden: true }
    },
    {
      path: '/',
      component: () => import('@/layout/Layout.vue'),
      redirect: '/dashboard',
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/dashboard/Analysis.vue'),
          meta: { title: '工作台', icon: 'Monitor' },
        },
        {
          path: 'permission',
          name: 'Permission',
          meta: { title: '组织管理', icon: 'Lock' },
          children: [
            {
              path: 'user',
              name: 'UserManagement',
              component: () => import('@/views/permission/UserManagement.vue'),
              meta: { title: '员工管理' }
            },
            {
              path: 'role',
              name: 'RoleManagement',
              component: () => import('@/views/permission/RoleManagement.vue'),
              meta: { title: '角色管理' }
            },
            {
              path: 'menu',
              name: 'MenuManagement',
              component: () => import('@/views/permission/MenuManagement.vue'),
              meta: { title: '菜单管理' }
            },
            {
              path: 'auth',
              name: 'AuthManagement',
              component: () => import('@/views/permission/AuthManagement.vue'),
              meta: { title: '权限管理' }
            }
          ]
        },
        {
          path: 'tenant',
          name: 'systemTenant',
          meta: { title: '租户管理', icon: 'Tools' },
          children: [
            {
              path: 'account',
              name: 'TenantManagement',
              component: () => import('@/views/systemTenant/TenantManagement.vue'),
              meta: { title: '租户管理' }
            },
            {
              path: 'wechat-config',
              name: 'TenantWechatConfig',
              component: () => import('@/views/systemTenant/TenantWechatConfig.vue'),
              meta: { title: '租户微信配置'}
            },
            {
              path: 'pay-config',
              name: 'TenantPayConfig',
              component: () => import('@/views/systemTenant/TenantPayConfig.vue'),
              meta: { title: '租户支付配置'}
            },
            // {
            //   path: 'log',
            //   name: 'Log',
            //   component: () => import('@/views/system/Log.vue'),
            //   meta: { title: '操作日志', icon: 'Document' }
            // },
            // {
            //   path: 'config',
            //   name: 'Config',
            //   component: () => import('@/views/system/Config.vue'),
            //   meta: { title: '系统配置', icon: 'Tools' }
            // }
          ]
        },
        {
          path: 'system',
          name: 'System',
          meta: { title: '系统管理', icon: 'Setting' },
          children: [
            {
              path: 'pay-config',
              name: 'PayConfig',
              component: () => import('@/views/system/PayConfig.vue'),
              meta: { title: '支付通道管理' }
            },
          ]
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  if (to.path === '/login') {
    if (token) {
      next('/')
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router 